package com.uft.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uft.model.dao.PreferencesDAO;
import com.uft.model.dao.UsersDAO;
import com.uft.model.entity.Preferences;
import com.uft.model.entity.Profile;
import com.uft.model.entity.Users;

@Controller
public class UserController {
	 @Autowired
	 private UsersDAO userDao;
	 
	 @RequestMapping(value="/getUserList", method = RequestMethod.POST)
	 @ResponseBody
	 public String getUserList(HttpServletRequest request) {
		 String result="";
			List<Users> usersList=new ArrayList<Users>();
			try{
				usersList=userDao.getUserList();
				ObjectMapper mapper = new ObjectMapper();
				String listData=mapper.writeValueAsString(usersList);
				//System.out.println("List dat: "+listData);
				result="{\"Result\":\"OK\",\"Records\":"+listData+"}";
			}
			catch(Exception ex){
				result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
				ex.printStackTrace();
			}
			
			return result;
	 }
	 @RequestMapping(value="/addUser", method = RequestMethod.POST)
		@ResponseBody
		public String addUser(Users user,HttpServletRequest request){
			String result="";
			try{
				//Users user= getBeanFromRequest(request);
				System.out.println("Username Name: "+user.getUsername());
				userDao.addUser(user);
				ObjectMapper mapper = new ObjectMapper();
				String listData=mapper.writeValueAsString(user);
				result="{\"Result\":\"OK\",\"Record\":"+listData+"}";
			}catch(Exception ex){
				result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
				ex.printStackTrace();
				
			}
			
			return result;
		}
	 @RequestMapping(value="/updateUser", method = RequestMethod.POST)
		@ResponseBody
		public String updateUser(Users user,HttpServletRequest request){
			String result="";
			try{
			//	Users user= getBeanFromRequest(request);
				System.out.println("Username: "+user.getUsername());
				userDao.updateUser(user);
				result="{\"Result\":\"OK\"}";
			}catch(Exception ex){
				result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
				ex.printStackTrace();
				
			}
			return result;
			
		}
	 @RequestMapping(value="/deleteUser", method = RequestMethod.POST)
		@ResponseBody
		public String deleteUser(@RequestParam("userid") int userid,HttpServletRequest request){
			String result="";
			try{
				
				System.out.println("user id to delete: "+userid);
				userDao.deleteUserById(userid);
				result="{\"Result\":\"OK\"}";
				
			}catch(Exception ex){
				result="{\"Result\":\"ERROR\",\"Message\":\""+ex.getMessage()+"\"}";
				ex.printStackTrace();
				
			}
			return result;
			
		}
	 
	 
//	 public Users getBeanFromRequest(HttpServletRequest request){
//			Users user= new Users();
//			if(request.getParameter("userid")!=null){	
//				   int userid=Integer.parseInt(request.getParameter("userid"));
//				   user.setUserid(userid);;
//			 }
//			if(request.getParameter("username")!=null){	
//				   String username=request.getParameter("username");
//				   user.setUsername(username);
//			 }
//			if(request.getParameter("password")!=null){	
//				   String password=request.getParameter("password");
//				   user.setPassword(password);;
//			 }
//			if(request.getParameter("groups")!=null){	
//				   String groups=request.getParameter("groups");
//				   user.setGroups(groups);;
//			 }
//			if(request.getParameter("enabled")!=null){	
//				   String enabled=request.getParameter("enabled");
//				   user.setEnabled(Boolean.valueOf(enabled));
//			 }
//			if(request.getParameter("admin")!=null){	
//				   String admin=request.getParameter("admin");
//				   user.setAdmin(Boolean.valueOf(admin));
//			 }
//			return user;
//		}
}
